package com.jack.lib.ui.adapter

import androidx.annotation.LayoutRes
import androidx.viewpager.widget.PagerAdapter
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
import com.jack.lib.ui.adapter.JViewHolder

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/29 13:29
 */
abstract class JPAdapter<T>(
    private var mList: MutableList<T>?,
    @param:LayoutRes private val mLayout: Int
) : PagerAdapter() {
    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(container.context).inflate(mLayout, container, false)
        container.addView(view)
        val viewHolder = JViewHolder(view)
        convert(viewHolder, mList!![position], position)
        return view
    }

    fun setData(index: Int, bean: T) {
        mList!![index] = bean
    }

    fun setNewData(list: MutableList<T>?) {
        mList = list
    }

    val data: List<T>?
        get() = mList

    override fun getCount(): Int {
        return if (mList == null) 0 else mList!!.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    protected abstract fun convert(helper: JViewHolder?, bean: T, position: Int)
}